package com.bristua.mall.user.biz;

import com.bristua.mall.common.bean.PageBean;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserCashTakeMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.TUserCashTake;
import com.bristua.mall.user.model.condition.TUserAccountExample;
import com.bristua.mall.user.model.condition.TUserCashTakeExample;
import com.bristua.mall.user.model.vo.QueryUserTakeCashVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.CachedIntrospectionResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/26.
 */
@Service
public class QueryUserWalletBiz {

    @Resource
    private TUserAccountMapper tUserAccountMapper;

    @Resource
    private TUserCashTakeMapper tUserCashTakeMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public Map<String,Object> queryUserWallet(int pageNo,int pageSize){
        TUser userSession = (TUser) cacheUtil.getUserSession();

        Map<String,Object> result = new HashMap();
        TUserAccountExample tUserAccountExample = new TUserAccountExample();
        tUserAccountExample.createCriteria().andUserIdEqualTo(userSession.getUserId());//用户ID

        List<TUserAccount> tUserAccounts = tUserAccountMapper.selectByExample(tUserAccountExample);

        if(tUserAccounts!=null && tUserAccounts.size()>0){
            result.put("balanceAmt",tUserAccounts.get(0).getUserIntegral());
        }else{
            result.put("balanceAmt","0");
        }

        TUserCashTakeExample tUserCashTakeExample = new TUserCashTakeExample();
        tUserCashTakeExample.createCriteria().andUserIdEqualTo(userSession.getUserId());//用户ID
        Page<TUserCashTake> objects = PageHelper.startPage(pageNo,pageSize);
        List<TUserCashTake> tUserCashTakes = tUserCashTakeMapper.selectByExample(tUserCashTakeExample);
        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            List<QueryUserTakeCashVO> queryUserTakeCashVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TUserCashTake tUserCashTake = (TUserCashTake) data.getList().get(i);
                QueryUserTakeCashVO queryUserTakeCashVO = new QueryUserTakeCashVO();
                BeanUtils.copyProperties(tUserCashTake,queryUserTakeCashVO);
                queryUserTakeCashVOS.add(queryUserTakeCashVO);
//                tDevice.setIsOnline(DeviceCode.getValueByCode(Integer.valueOf(tDevice.getIsOnline())));
            }
            data.getList().clear();
            data.getList().addAll(queryUserTakeCashVOS);
        }

        result.put("totalCount",data.getTotalCount());
        result.put("list",data.getList());
        return result;
    }
}
